<?php

namespace app\common\service;

use app\common\controller\Send;

use Emmetltd\AliyunCore\Profile\DefaultProfile; 
use Emmetltd\AliyunCore\DefaultAcsClient;

class Mps
{
    use Send;

    protected $accessKeyId;
    protected $accessKeySecret;
    protected $regionId;
    protected $endpoint;
    protected $ossLocation;
    // 存储空间名称
    protected $bucket;
    public function __construct()
    {
//        $config = [
//            'access_key_id' => '12',
//            'access_key_secret' => '34',
//            'endpoint' => '22',
//            'bucket' => 'testtest'
//        ];
        $config = config('app.aliyun_oss');
        $this->accessKeyId = $config['access_key_id'];
        $this->accessKeySecret = $config['access_key_secret'];
        $this->endpoint = $config['endpoint'];
        $this->bucket = $config['bucket'];
        $this->regionId = "cn-hangzhou";
        $this->ossLocation = "oss-cn-hangzhou";
    }



    public function transfer($inputObject,$outObject,$templateId){
        $clientProfile = DefaultProfile::getProfile(
            $this->regionId,                   # 您的 Region ID
            $this->accessKeyId,                   # 您的 AccessKey ID
            $this->accessKeySecret               # 您的 AccessKey Secret
        );
        $client = new DefaultAcsClient($clientProfile);
        # 创建API请求并设置参数
        $request = new Mts\SubmitJobsRequest();
        $request->setAcceptFormat('JSON');
        # Input
        $input = array('Location' => $this->ossLocation,
                       'Bucket' => $this->bucket,
                       'Object' => urlencode($inputObject));
        $request->setInput(json_encode($input));
        # Output
        $output = array('OutputObject' => urlencode($outObject));
        # Ouput->Container
        $output['Container'] = array('Format' => 'mp4');
        # Ouput->Video
        $output['Video'] = array('Codec' =>'H.264','Bitrate' => 1500,'Width' => 1280,'Fps' => 25);
        # Ouput->Audio
        $output['Audio'] = array('Codec' => 'AAC','Bitrate' => 128,'Channels' => 2,'Samplerate' => 44100);
        # Ouput->TemplateId
        $output['TemplateId'] = $templateId;
        $outputs = array($output);
        $request->setOUtputs(json_encode($outputs));
        $request->setOutputBucket($oss_bucket);
        $request->setOutputLocation($oss_location);
        # PipelineId
        $request->setPipelineId($pipeline_id);
        # 发起请求并处理返回
        try {
            $response = $client->getAcsResponse($request);
                print 'RequestId is:' . $response->{'RequestId'} . "\n";;
            if ($response->{'JobResultList'}->{'JobResult'}[0]->{'Success'}) {
                print 'JobId is:' .
                $response->{'JobResultList'}->{'JobResult'}[0]->{'Job'}->{'JobId'} . "\n";
                return self::sendResult(self::$okCode);
            } else {
                print 'SubmitJobs Failed code:' .
                $response->{'JobResultList'}->{'JobResult'}[0]->{'Code'} .
                ' message:' .
                $response->{'JobResultList'}->{'JobResult'}[0]->{'Message'} . "\n";
                return self::sendResult(128, $e->getMessage());
            }
        } catch(ServerException $e) {
            print 'Error: ' . $e->getErrorCode() . ' Message: ' . $e->getMessage() . "\n";
            return self::sendResult(128, $e->getMessage());
        } catch(ClientException $e) {
            print 'Error: ' . $e->getErrorCode() . ' Message: ' . $e->getMessage() . "\n";
            return self::sendResult(128, $e->getMessage());
        }
    }

    /**
     * 上传字符串到OSS
     * @param string $object 文件名称
     * @param string $content 文件内容
     * @param null $options
     * @return array
     */
    public function put($object, $content, $options = NULL)
    {
        try {
            $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
            $ossClient->putObject($this->bucket, $object, $content, $options);
            return self::sendResult(self::$okCode);
        } catch (\Exception $e) {
            return self::sendResult(128, $e->getMessage());
        }
    }

    /**
     * 上传文件到oss
     * @param string $object 文件名称
     * @param string $file <yourLocalFile>由本地文件路径加文件名包括后缀组成，例如/users/local/myfile.txt
     * @param null $options
     * @return array
     */
    public function upload($object, $file, $options = NULL)
    {
        try {
            $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
            $ossClient->uploadFile($this->bucket, $object, $file, $options);
            return self::sendResult(self::$okCode);
        } catch (\Exception $e) {
            return self::sendResult(128, $e->getMessage());
        }
    }
}